package com.xiaoyu.chap2.OOM;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiaoyu on 16/3/31.
 *
 * VM Args: -Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError
 * 将堆的最小值-Xms参数与最大值-Xmx参数设置成一样可以避免堆自动拓展
 * -XX:+HeapDumpOnOutOfMemoryError可以让虚拟机在出现内存溢出异常时Dump出当前的内存堆转储快照
 */
public class HeapOOM {
    static class OOMObject{

    }

    public static void main(String[] args) {
        List<OOMObject> list  = new ArrayList<OOMObject>();
        while (true){
            list.add(new OOMObject());
        }

    }
}
/*
output:
    java.lang.OutOfMemoryError: Java heap space
    Dumping heap to java_pid45667.hprof ...
    Heap dump file created [27893356 bytes in 0.416 secs]
    Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	    at java.util.Arrays.copyOf(Arrays.java:3204)
	    at java.util.Arrays.copyOf(Arrays.java:3175)
	    at java.util.ArrayList.grow(ArrayList.java:246)
	    at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:220)
	    at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:212)
	    at java.util.ArrayList.add(ArrayList.java:443)
	    at com.xiaoyu.chap2.OOM.HeapOOM.main(HeapOOM.java:19)
	    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	    at java.lang.reflect.Method.invoke(Method.java:483)
	    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

 */